import axios from 'axios';
import {Http} from "@/config/HttpConfig";
import {router} from '../router/index';

export default function ajax(api, method = 'get', data = null) {
  // api地址
  let url = `${Http.url}/${api}`;
  return new Promise((resolve, reject) => {
    let Authorization = storage.get('token');
    axios({
      headers: {
        'Content-Type': 'application/json',
        'Authorization': api === 'login' ? '' : Authorization
      },
      method,
      url,
      [method === 'get' ? 'params' : 'data']: data
    })
      .then(response => response.data)
      .then(data => {
        if (!data) {
          reject(data);
        } else if (data.status === 200) {
          resolve(data);
        } else if (data.status === 600) {
        } else {
        }
      })
      .catch(error => {
        reject(error);
      });
  });
};
